//
//  CategoryController.m
//  ProductB
//
//  Created by 田阳 on 16/8/5.
//  Copyright © 2016年 田阳. All rights reserved.
//

#import "CategoryController.h"
#import "CategoryCell.h"
#import "FirstCell.h"
#import "CategoryModel.h"
@interface CategoryController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

//存放title的数组
@property(nonatomic,strong) NSMutableArray *titleArray;
@end

@implementation CategoryController

- (NSMutableArray *)titleArray
{
    if (!_titleArray) {
        self.titleArray = [[NSMutableArray alloc] init];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatCollectionView];
    [self dataRequestWithURL];
}

- (void)creatCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _flowLayout.headerReferenceSize = CGSizeMake(kScreenWidth, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 114 - kScreenHeight / 20) collectionViewLayout:_flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"CategoryCell" bundle:nil] forCellWithReuseIdentifier:@"categoryCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"FirstCell" bundle:nil] forCellWithReuseIdentifier:@"firstCell"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [self.view addSubview:_collectionView];
}

#pragma mark 数据解析
- (void)dataRequestWithURL{
    [NetWorkrequestManage requestWithType:GET url:KCategoryURL parameters:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *array = dic[@"list"];
        for (NSDictionary *dict in array) {
            CategoryModel *model = [[CategoryModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.titleArray addObject:model];
        }
        NSLog(@"%ld",_titleArray.count);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    } error:nil];
}

#pragma mark ----colllectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.titleArray.count / 5 + 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%ld",section);
    if (section == 0) {
        return 1;
    }else if (section == 5) {
        return 4;
    }else{
        return 6;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    headerView.alpha = 0.3;
    headerView.backgroundColor = [UIColor grayColor];
    return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(kScreenWidth, 150);
    }else{
        return CGSizeMake(kScreenWidth / 2, 40);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"categoryCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        FirstCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
        return myCell;
    }else if (indexPath.section == 1) {
        CategoryModel *model = self.titleArray[indexPath.row];
        [cell setDataWithModel:model];
    }else if (indexPath.section == 2){
        CategoryModel *model = self.titleArray[indexPath.row + 6];
        [cell setDataWithModel:model];
    }else if (indexPath.section == 3){
        CategoryModel *model = self.titleArray[indexPath.row + 12];
        [cell setDataWithModel:model];
    }else if (indexPath.section == 4){
        CategoryModel *model = self.titleArray[indexPath.row + 18];
        [cell setDataWithModel:model];
    }else{
        CategoryModel *model = self.titleArray[indexPath.row + 24];
        [cell setDataWithModel:model];
    }
        return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
